package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.enums.SelectionMode;
import com.jdy.haoduoaiteacher.model.StudentDeviceModel;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.view.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;
import java.util.HashMap;

//  my love  story list adapter
@SuppressLint("InflateParams")
public class ListDevicesAdapter extends BaseAdapter {
    private Activity mContext;
    private LayoutInflater mInflater;
    private ArrayList<StudentDeviceModel> mList;
    private DisplayImageOptions options;
    private boolean isEditor = false;

    public void updateData(ArrayList<StudentDeviceModel> ll) {
        this.mList = ll;
        notifyDataSetChanged();
    }

    public boolean isPublishPhoto() {
        return isPublishPhoto;
    }

    public void setPublishPhoto(boolean publishPhoto) {
        isPublishPhoto = publishPhoto;
    }

    private boolean isPublishPhoto = false;


    private int highLightPosition = -1; // 正在播放

    public int getHighLightPosition() {
        return highLightPosition;
    }

    public void setHighLightPosition(int highLightPosition) {
        this.highLightPosition = highLightPosition;
        notifyDataSetChanged();
    }

    public SelectionMode SelectMode = SelectionMode.NONE;

    @SuppressWarnings("static-access")
    public void setSelectMode(SelectionMode sm) {
        SelectMode = sm;

        if (mList != null && !mList.isEmpty()) {
            if (SelectMode == SelectMode.SELECT_ALL) {
                for (StudentDeviceModel item : mList) {
                    item.isChecked = true;
                }
            } else if (SelectMode == SelectMode.DESELECT_ALL) {
                for (StudentDeviceModel item : mList) {
                    item.isChecked = false;
                }
            }
        }
    }

    public void remove(StudentDeviceModel object) {
        mList.remove(object);
        notifyDataSetChanged();
    }

    public ArrayList<StudentDeviceModel> getList() {
        return mList;
    }

    public void removeSelection() {
        for (StudentDeviceModel item : mList) {
            item.isChecked = false;
        }

        notifyDataSetChanged();
    }

    public void setEditor(boolean iseditor) {
        this.isEditor = iseditor;
    }

    public ListDevicesAdapter(Activity context, ArrayList<StudentDeviceModel> list) {
        this.mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
        // 用 displayImage 加载图片 的 配置参数
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.girlpic) // 下载 失败 显示的 图片
                .cacheInMemory(true) // 缓存
                .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
                .build();
        // 创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
                mContext).writeDebugLogs() // 打印 log 信息
                .build();
        ImageLoader.getInstance().init(configuration);
       ;
    }

    public HashMap<String, String> mDeviceStatusMap;
    public HashMap<String, Integer> mUnreadMsgMap;

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    public View getSelectView() {
        return mSelView;
    }

    private View mSelView;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        StudentDeviceModel item = mList.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listitem_device, null);
            holder.img_headicon = (RoundImageView) convertView.findViewById(R.id.img_headicon);
            holder.tv_msgnum = (TextView) convertView.findViewById(R.id.tv_msgnum);
            holder.view_root = convertView.findViewById(R.id.view_root);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);

            holder.img_onlineflag = (TextView) convertView.findViewById(R.id.img_onlineflag);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (Tools.isNotEmpty(item) && Tools.isNotEmpty(item.machine) && Tools.isNotEmpty(item.machine.avatar)) {
            ImageLoader.getInstance().displayImage(item.machine.avatar,
                    holder.img_headicon, options);
        }

        if (mUnreadMsgMap != null) {
            if (mUnreadMsgMap.containsKey(item.machine_id.toLowerCase())) {
                int msgNum = 0;
                msgNum = mUnreadMsgMap.get(item.machine_id.toLowerCase());
                if (msgNum > 0) {
                    holder.tv_msgnum.setText("" + msgNum);
                    holder.tv_msgnum.setVisibility(View.VISIBLE);
                } else {
                    holder.tv_msgnum.setVisibility(View.GONE);
                }
            } else {
                holder.tv_msgnum.setVisibility(View.GONE);
            }
        } else {
            holder.tv_msgnum.setVisibility(View.GONE);
        }

        if (mDeviceStatusMap != null) {
            if (mDeviceStatusMap.containsKey(item.machine_id.toLowerCase())) {
                String strStatus = "-2";
                strStatus = mDeviceStatusMap.get(item.machine_id.toLowerCase());
                if (Tools.isNotEmpty(strStatus)) {
                    if (strStatus.equals("-1") || strStatus.equals("-2")) {
                        // 离线
                        holder.img_headicon.setDrawGray(true);
                        holder.img_headicon.invalidate();
                    } else if (strStatus.equals("1")) {
                        // 在线
                        holder.img_headicon.setDrawGray(false);
                        holder.img_headicon.invalidate();
                    }
                }
            }
        }

        if (highLightPosition == position) {
            holder.view_root.setBackgroundColor(0xffd7d7d7);
        } else {
            holder.view_root.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
        }

        holder.tv_name.setText(item.nick_name);

        return convertView;
    }

    public final float[] BT_SELECTED = new float[]{1, 0, 0, 0, -50, 0, 1, 0,
            0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0};

    private static class ViewHolder {
        private RoundImageView img_headicon;
        private View view_root;
        private TextView tv_msgnum, tv_name, img_onlineflag;
    }
}
